package de.tmahr.android;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import de.tmahr.android.logger.LogActivity;

import android.content.res.AssetManager;
import android.os.Bundle;

public class AssetsTest extends LogActivity
{
	public AssetsTest()
	{
		super("AssetsTest");
	}
	
	@Override
	public void onCreate(Bundle b)
	{
		super.onCreate(b);
		String fileName = "texte/meintext.txt";
		
		AssetManager manager = getAssets();
		InputStream is = null;
		try
		{
			is = manager.open(fileName);
			String s = loadText(is);
			log(s);
		}
		catch(IOException e)
		{
			log("Kann Datei " + fileName + " nicht öffnen");
		}
	}
	
	private String loadText(InputStream is) throws IOException
	{
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		byte[] bytes = new byte[4096];
		int len = 0;
		while((len=is.read(bytes))>0)
		{
			bs.write(bytes,0,len);
		}
		return new String(bs.toByteArray(),"UTF8");
	}
}
